//
//  PlayerTargetingModule.m
//  TengineTwo
//
//  Created by StandardUser on 28/02/2012.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//


#import "PlayerTargetingModel.h"
#import "CGPointExtension.h"

@interface PlayerTargetingModel()

@property(readwrite, nonatomic, retain) NSMutableArray *targetSequence;

@end

@implementation PlayerTargetingModel

@synthesize maxTargets;
@synthesize targetSequence;

- (id)init {
    self = [super init];
    if (self) {
        maxTargets = 3;
        targetSequence = [[NSMutableArray alloc] init];
    }
    return self;
}

- (void)dealloc {
    
    [targetSequence dealloc];
    [super dealloc];
}

#pragma mark accessors

-(EntityModel*)currentTarget
{
    EntityModel * target = nil;
    if([targetSequence count] > 0) 
        target = [targetSequence objectAtIndex:0];
    
    return target;
}

#pragma -

-(void)acquireTargetsFrom:(NSMutableArray*)entities
{
    [entities sortUsingSelector:@selector(tOffset)];
    NSEnumerator *iterator = [entities objectEnumerator];
    EntityModel *e;
    
    while ((e = (EntityModel*)[iterator nextObject]) && [targetSequence count] < maxTargets)
    {
        if(e.isAlive) [targetSequence addObject:e];
    }
}

-(void)clearTargets
{
    [targetSequence removeAllObjects];
}

-(CGPoint) getPositionOfCurrentTarget
{
    if([targetSequence count] <= 0) return ccp(0,0);
        
    EntityModel *e = [targetSequence objectAtIndex:0];
    return e.position;
}

@end
